/*
 * Copyright (c) 2020-2099 the original author or authors. All rights reserve.
 */
package org.tianyun.cloud.exception;

/**
 * 业务异常
 * <p>
 * 示例：
 *
 * <pre>
 * throw new BusinessException("5004", "password");
 *
 * 前端呈现：参数“password”不能为空
 * </pre>
 * </p>
 *
 * @auther ebert_chan
 */
public class BusinessException extends RuntimeException {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 1L;

    /**
     * 异常码
     */
    private String code;

    /**
     * 异常参数
     */
    private Object[] args;

    // public constructors

    public BusinessException(String code) {
        this.code = code;
    }

    public BusinessException(String code, Throwable cause) {
        super(cause);
        this.code = code;
    }

    public BusinessException(String code, String message) {
        this(code, null, message);
    }

    public BusinessException(String code, Object... args) {
        this(code, null, args);
    }

    public BusinessException(String code, Throwable cause, Object... args) {
        super(cause);
        this.code = code;
        this.args = args;
    }

    // getter and setter

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public Object[] getArgs() {
        return args;
    }

    public void setArgs(Object[] args) {
        this.args = args;
    }

    /*
     * @see java.lang.Throwable#getMessage()
     */
    @Override
    public String getMessage() {
        StackTraceElement stackTraceElement = this.getStackTrace()[0];
        StringBuilder sb = new StringBuilder();
        sb.append("[").append(this.getCode()).append("]business exception. @");
        sb.append(stackTraceElement.getClassName()).append(":").append(stackTraceElement.getLineNumber());
        return sb.toString();
    }

}
